package com.focus.xyz.other.pages.flight;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import com.focus.xyz.core.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.util.List;

/**
 * Created by chenmeng on 2016/6/1.
 */
public class FlightPage extends BasePage {
    private Action action;
    private String url = "http://www.variflight.com/";

    @FindBy(xpath = "//div[@class='select']")
    private WebElement selectSearchType;
    @FindBy(xpath = "//li[@class='byCity']")
    private WebElement byCity;
    @FindBy(xpath = "//li[@class='byNum']")
    private WebElement byNum;
    @FindBy(id = "byNumInput")
    private WebElement byNumInput;
    @FindBy(id = "numDatepicker")
    private WebElement numDatepicker;
    @FindBy(id = "byNumBtn")
    private WebElement byNumBtn;


    /*
    * flight info
    * */
    @FindBy(xpath = "//div[@class='state']")
    private WebElement state;
    @FindBy(xpath = "//div[@class='cir_l curr']/span")
    private WebElement depart;
    @FindBy(xpath = "//div[@class='cir_r']/span")
    private WebElement dest;
    @FindBy(xpath = "//div[@class='tit']/span/b")
    private WebElement company;

    /*
    * flight plan
    * */
    @FindBy(xpath = "//div[contains(@class,'f_title_a')]/span[@class='date']")
    private WebElement planDepartTime;
    @FindBy(xpath = "//div[contains(@class,'f_title_c')]/span[@class='date']")
    private WebElement planArriveTime;

    @FindBy(xpath = "//div[contains(@class,'f_title_a')]/h2")
    private WebElement departTerminal;
    @FindBy(xpath = "//div[contains(@class,'f_title_c')]/h2")
    private WebElement arriveTerminal;


    /*
    * plane info
    * */
    @FindBy(xpath = "//div[@class='p_info']/ul")
    private WebElement planInfo;

    /*
    * line info
    * */
    @FindBy(xpath = "//div[@class='p_ti']")
    private WebElement lineInfo;


    public FlightPage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        PageFactory.initElements(driver,this);
        openPage(url);
    }

    public void selectType(String type) throws Exception{
        action.click(selectSearchType);
        if("1".equals(type)){
            action.click(byCity);
        }else if("2".equals(type)){
            action.click(byNum);
        }
    }

    public void inputFightDate(String date) throws Exception {
        action.removeAttribute(numDatepicker,"readonly");
        action.inputText(numDatepicker,date);
    }

    public void inputFightNo(String no) throws Exception {
        action.inputText(byNumInput,no);
    }

    public void clickSearchBtn() throws Exception {
        action.click(byNumBtn);
    }

    public void searchByNo(String date, String no) throws Exception {
        inputFightDate(date);
        inputFightNo(no);
        clickSearchBtn();
    }

    public FlightObject getFlightInfo() throws Exception{
        action.waitForPageLoad();
        FlightObject object = new FlightObject();
        String tmp = action.getText(By.xpath("//div[@class='tit']/span/b"));
        if(StringUtils.isNotBlank(tmp)){
            String[] cc = tmp.split(" ");
            if(cc!=null&&cc.length>1){
                object.setComnany(cc[0]);
                object.setFltNo(cc[1]);
            }
        }
        tmp = action.getText(By.xpath("//div[@class='state']"));
        if(StringUtils.isNotBlank(tmp)){
            object.setState(tmp);
        }
        action.waitForElementPresent(By.xpath("//div[@class='p_ti']"));
        List<WebElement> linfos = lineInfo.findElements(By.tagName(Constants.TAG_SPAN));
        if(linfos!=null&&linfos.size() == 2){
            tmp = linfos.get(0).getText().trim();
            if(StringUtils.isNotBlank(tmp)){
                object.setMileage(tmp.substring(2));
            }
            tmp = linfos.get(1).getText().trim();
            if(StringUtils.isNotBlank(tmp)){
                object.setDuration(tmp.substring(2));
            }
        }

        List<WebElement> planInfos = planInfo.findElements(By.tagName(Constants.TAG_LI));
        if(planInfos!=null&&planInfos.size() == 4){
            tmp = planInfos.get(0).getText().trim();
            object.setPlaneType(tmp.substring(tmp.indexOf("：") + 1));
            tmp = planInfos.get(1).getText().trim();
            object.setPlaneAge(tmp.substring(tmp.indexOf("：") + 1));
            tmp = planInfos.get(2).getText().trim();
            object.setOnTimeRate(tmp.substring(tmp.indexOf("：") + 1));
            tmp = planInfos.get(3).getText().trim();
            String islate = tmp.substring(0,5);
            object.setIsLate(islate);
            String latetime = tmp.substring(5,tmp.length()-2);
            object.setLateTime(latetime);
        }

        tmp = departTerminal.getText().trim();
        if(StringUtils.isNotBlank(tmp)){
            object.setDepartPlace(tmp);
        }

        tmp = arriveTerminal.getText().trim();
        if(StringUtils.isNotBlank(tmp)){
            object.setDestPlace(tmp);
        }

        tmp = planDepartTime.getText().trim();
        if(StringUtils.isNotBlank(tmp)){
            tmp = tmp.substring(tmp.length()-5);
            object.setPlanDepartTime(tmp);
        }

        tmp = planArriveTime.getText().trim();
        if(StringUtils.isNotBlank(tmp)){
            tmp = tmp.substring(tmp.length()-5);
            object.setPlanArriveTime(tmp);
        }

        return object;
    }
}
